package servlet;

import modle.User;
import service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "UserLoginServlet", value = "/user_login")
public class UserLoginServlet extends HttpServlet {
    private UserService userService = new UserService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        String ue = request.getParameter("ue");
        String password = request.getParameter("password");

        User user = userService.login(ue, password);
        if (user == null) {

            request.setAttribute("failMsg","用户名、邮箱或者密码错误，请重新登录!");
            request.getRequestDispatcher("/user_login.jsp").forward(request,response);
        }else {

            request.getSession().setAttribute("user",user);
            request.getRequestDispatcher("/user_center.jsp").forward(request,response);
        }


    }
}
